import  * as fabric from 'fabric';
import { resolveOrigin } from '../../utils/resolveOrigin';

export const getActionFromCorner = (alreadySelected, corner, e, target) => {
  if (!corner || !alreadySelected) return 'drag';
  const control = target.controls[corner];
  return control.getActionName(e, control, target);
};

export function isTransformCentered(transform) {
  return transform.originX === 'center' && transform.originY === 'center';
}

export function invertOrigin(origin) {
  return -resolveOrigin(origin) + 0.5;
}

export const isLocked = ( target, lockingKey) => target[lockingKey];

export function normalizePoint(target, point, originX, originY) {
  const center = target.getRelativeCenterPoint(),
    p = typeof originX !== 'undefined' && typeof originY !== 'undefined' 
    ? target.translateToGivenOrigin(center,'center','center', originX,originY) 
    : new fabric.Point(target.left, target.top),
    p2 = target.angle 
    ? point.rotate(-fabric.util.degreesToRadians(target.angle), center) 
    : point;
  return p2.subtract(p);
}